package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :rain
 * @description 商品的详情，分页查询商品的列表，查询推荐商品
 * @since 2022/07/09 10:15
 */
@RestController
public class ShoppingDetailAndListController {

    @Autowired
    IDetailService iDetailService;

    @GetMapping("/shopping/product/{id}")
    public ResponseData shoppingDetail(@PathVariable Long id) {
        //生成request对象
        ProductDetailRequest request = new ProductDetailRequest();
        //将请求参数封装到request对象中
        request.setId(id);
        //查询商品详情
        ProductDetailResponse response = iDetailService.getProductDetail(request);
        //对返回的response进行判别
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            return new ResponseUtil().setData(response.getProductDetailDto());
        }

        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("/shopping/goods")
    public ResponseData shoppingGoods(AllProductRequest request) {
        AllProductResponse response = iDetailService.getAllProduct(request);
        //对返回的response进行判别
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            return new ResponseUtil().setData(response);
        }
        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
}
